package cn.jjaw.command.worker.command;

import java.util.ArrayList;
import java.util.List;

/**
 * 命令工作类
 */
public class CommandWorker {
    List<CommandExecute> commandExecutes = new ArrayList<>();

    /**
     * 执行一条命令,已经被拆分成数组
     */
    public ExecuteResult execute(String[] command){
        for (CommandExecute commandExecute : commandExecutes) {
            boolean reg = false;
            try{
                reg = commandExecute.reg(command);
            }catch (Throwable throwable){
                throwable.printStackTrace();
            }
            //如果成功匹配就执行
            if (reg) {
                try {
                    //执行成功就返回结果
                    return new ExecuteResult(
                            true,
                            true,
                            commandExecute.execute(command),
                            null
                    );
                }catch (Throwable throwable){
                    //返回错误
                    return new ExecuteResult(
                            true,
                            false,
                            null,
                            throwable
                    );
                }
            }
        }
        //如果全部都不匹配
        return new ExecuteResult(false,false,null,null);
    }
    /**
     * 执行一条命令
     */
    public ExecuteResult execute(String command){
        return execute(command.split(" "));
    }

    /**
     * 添加一条命令
     */
    public void add(CommandExecute command){
        commandExecutes.add(command);
    }


}
